//
//  PackageListCell.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/12/4.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "PackageListCell.h"
#import "CommonPackageModel.h"

@interface PackageListCell ()
@property(nonatomic, strong) UIImageView *coverImageView;
@property(nonatomic, strong) UILabel *nameLabel;
@property(nonatomic, strong) UILabel *priceLabel;
@end

@implementation PackageListCell
- (void)makeUI {
    [super makeUI];
    [self.contentView addSubview:self.coverImageView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.priceLabel];
    self.coverImageView.frame = CGRectMake(0, 0, self.width, ceil((kScreenWidth - 34) * 0.5 * 0.94));
    [self.coverImageView pp_clipCorners:UIRectCornerTopLeft | UIRectCornerTopRight radius:8];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.right.mas_equalTo(-8);
        make.top.mas_equalTo(self.coverImageView.mas_bottom).mas_equalTo(10);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.right.mas_equalTo(-8);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).mas_equalTo(9);
    }];
}
#pragma mark - setter
- (void)setData:(CommonPackageModel *)data {
    _data = data;
    NSString *mainUrl = [TT_OSSManager reSourceUrl:data.fangUrl type:BA_resizeType_pro];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:mainUrl] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
            if (cacheType != SDImageCacheTypeMemory) {
                [UIView transitionWithView:self.coverImageView duration:0.61 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                    [self.coverImageView setImage:image];
                    self.coverImageView.alpha = 1.0;
                } completion:nil];
            } else {
                self.coverImageView.alpha = 1.0;
                [self.coverImageView setImage:image];
            }
        }
    }];
    self.nameLabel.text = data.goodsName;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:data.goodsActivePrice]];
}
- (void)setData2:(CommonPackageModel *)data {
    _data2 = data;
    NSString *mainUrl = [TT_OSSManager reSourceUrl:data.url type:BA_resizeType_pro];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:mainUrl] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
            if (cacheType != SDImageCacheTypeMemory) {
                [UIView transitionWithView:self.coverImageView duration:0.61 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                    [self.coverImageView setImage:image];
                    self.coverImageView.alpha = 1.0;
                } completion:nil];
            } else {
                self.coverImageView.alpha = 1.0;
                [self.coverImageView setImage:image];
            }
        }
    }];
    self.nameLabel.text = data.goodsName;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:data.goodsActivePrice]];
}
#pragma mark - lazy
- (UIImageView *)coverImageView {
    if (!_coverImageView) {
        UIImageView *iv = [[UIImageView alloc]init];
        iv.contentMode = UIViewContentModeScaleAspectFill;
        _coverImageView = iv;
    }
    return _coverImageView;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = kFontRegular(14);
        lab.textColor = rgba(47, 47, 52, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 2;
        _nameLabel = lab;
    }
    return _nameLabel;
}
- (UILabel *)priceLabel {
    if (!_priceLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = kFontMedium(14);
        lab.textColor = rgba(254, 118, 15, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        _priceLabel = lab;
    }
    return _priceLabel;
}
@end
